"use strict";
cc._RF.push(module, 'ee2a2U7DCJFnadxLRmUrlnS', 'level_goldMinerShopPage');
// scripts/levelBundles/level_goldMiner/page/level_goldMinerShopPage.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var PageBase_1 = require("../../../Game/Base/PageBase");
var gameEngine_1 = require("../../../engines/gameEngine");
var CsvFileKind_1 = require("../../../engines/libs/CsvFileKind");
var data_control_1 = require("../../../engines/libs/data_control");
var Level_goldMiner_shopItem_1 = require("../other/Level_goldMiner_shopItem");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var level_goldMinerShopPage = /** @class */ (function (_super) {
    __extends(level_goldMinerShopPage, _super);
    function level_goldMinerShopPage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.propItem = null;
        _this.propContent = null;
        _this.shopNode = null;
        _this.btnNode = null;
        _this.lb_curMoney = null;
        _this.level = null;
        _this.completeFun = null;
        return _this;
    }
    level_goldMinerShopPage.prototype.start = function () {
        this.shopAni();
    };
    level_goldMinerShopPage.prototype.bindLevel = function (level, completeFun) {
        this.level = level;
        this.completeFun = completeFun;
        this.loadShop();
        this.refrenshCurMoney();
    };
    level_goldMinerShopPage.prototype.shopAni = function () {
        var _this = this;
        cc.tween(this.shopNode).to(1, { x: 0 }, { easing: cc.easing.backOut }).call(function () {
            cc.tween(_this.btnNode).to(0.5, { opacity: 255 }).start();
        }).start();
    };
    level_goldMinerShopPage.prototype.loadShop = function () {
        var allPropIds = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.goldMinerProp);
        var moneyPropAry = [];
        var adProp = [];
        for (var _i = 0, allPropIds_1 = allPropIds; _i < allPropIds_1.length; _i++) {
            var id = allPropIds_1[_i];
            var prop = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.goldMinerProp, id);
            if (prop["ad_flag"] == "1") {
                adProp.push(id);
            }
            else {
                moneyPropAry.push(id);
            }
        }
        adProp = data_control_1.default.randomSortArray(adProp);
        moneyPropAry = data_control_1.default.randomSortArray(moneyPropAry);
        var propIds = [];
        var randCount = Math.floor(Math.random() * 2) + 2;
        var randIndex = Math.floor(Math.random() * randCount);
        for (var i = 0; i < randCount; i++) {
            var propId = null;
            if (randIndex == i) {
                propId = adProp.pop();
            }
            else {
                propId = moneyPropAry.pop();
            }
            propIds.push(propId);
        }
        for (var i = 0; i < propIds.length; i++) {
            var id = propIds[i];
            var item = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.goldMinerProp, id);
            var propItem = cc.instantiate(this.propItem);
            var ps = propItem.getComponent(Level_goldMiner_shopItem_1.default);
            ps.bindData(item, this.level);
            propItem.parent = this.propContent;
            propItem.on("touchend", this.onSelectOne, this);
        }
    };
    level_goldMinerShopPage.prototype.onSelectOne = function (e) {
        var _this = this;
        var node = e.target;
        var ns = node.getComponent(Level_goldMiner_shopItem_1.default);
        var nData = ns.data;
        if (ns.buyFlag) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("已售罄");
            return;
        }
        if (nData["ad_flag"] == "1") {
            // 需要看广告
            if (gameEngine_1.default.getServices().userSrv.getCoins() > 0) {
                gameEngine_1.default.getServices().userSrv.setCoins(-1, true);
                ns.onBuy();
                this.buyOneItem(ns, nData);
            }
            else {
                gameEngine_1.default.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(function () {
                    ns.onBuy();
                    _this.buyOneItem(ns, nData);
                }, function () {
                }, null, "\u770B\u5E7F\u544A\u83B7\u5F97\u9053\u5177" + nData["prop_name"]);
            }
        }
        else {
            if (this.level.curMoney >= ns.moneyNum) {
                ns.onBuy();
                this.level.curMoney -= ns.moneyNum;
                this.refrenshCurMoney();
                this.buyOneItem(ns, nData);
            }
            else {
                gameEngine_1.default.getModules().uiControlModule.showMessageBox("钱不够");
                return;
            }
        }
    };
    level_goldMinerShopPage.prototype.refrenshCurMoney = function () {
        this.lb_curMoney.string = this.level.curMoney.toString();
    };
    level_goldMinerShopPage.prototype.buyOneItem = function (item, data) {
        var _this = this;
        var icon = item.icon;
        var pos = this.node.convertToNodeSpaceAR(icon.node.convertToWorldSpaceAR(cc.v2()));
        icon.node.setParent(this.node);
        icon.node.setPosition(pos);
        //
        var targetPos = cc.v2(200, 159);
        //@ts-ignore
        cc.tween(icon.node).to(0.2, { position: targetPos }).to(0.2, { opacity: 0 }).call(function () {
            gameEngine_1.default.getManagers().goldMinerManager.addOneProp(Number(data["prop_type"]));
            if (data["prop_type"] == "4") {
                _this.level.showSpeedUpgrade();
            }
            if (item && item.node && item.node.isValid) {
                icon.node.setParent(item.node);
                icon.node.setPosition(0, 55);
            }
            else {
                icon.node.destroy();
                icon.node.removeFromParent();
            }
        }).start();
    };
    level_goldMinerShopPage.prototype.OnCloseBtn = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.completeFun && this.completeFun();
                _super.prototype.OnCloseBtn.call(this);
                return [2 /*return*/];
            });
        });
    };
    __decorate([
        property({ type: cc.Node, tooltip: "道具节点" })
    ], level_goldMinerShopPage.prototype, "propItem", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "道具content" })
    ], level_goldMinerShopPage.prototype, "propContent", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "大节点" })
    ], level_goldMinerShopPage.prototype, "shopNode", void 0);
    __decorate([
        property({ type: cc.Node, tooltip: "按钮节点" })
    ], level_goldMinerShopPage.prototype, "btnNode", void 0);
    __decorate([
        property({ type: cc.Label, tooltip: "当前金额文本" })
    ], level_goldMinerShopPage.prototype, "lb_curMoney", void 0);
    level_goldMinerShopPage = __decorate([
        ccclass
    ], level_goldMinerShopPage);
    return level_goldMinerShopPage;
}(PageBase_1.default));
exports.default = level_goldMinerShopPage;

cc._RF.pop();